package com.zzcm.channelinfo.receiver;

import java.util.ArrayList;

import com.android.installApp.InstallAppHelper;
import com.zzcm.channelinfo.control.SilentInstallationControl;
import com.zzcm.channelinfo.tools.Tools;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * 接收广播，启动service
 * @author 张少志
 * 本功能已经基本稳定
 */
public class ZZCMIReceiver extends BroadcastReceiver {
    
    /**
     * 显示LOG信息
     */
    private String TAG = this.getClass().getName();
    private boolean showLog = true;// true;//false;
    public void showLog(String tag, String msg) {
        if (showLog) {
        	Tools.showLog(TAG,tag+" : "+msg);
        }
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
    	if(intent==null)
    	{
    		return;
    	}
        String action = intent.getAction();
        showLog("onReceive", "onReceive ACTION IS " + action);
        if(action!=null&&!action.trim().equals(""))
        {
        	String showLog = intent.getStringExtra(SilentInstallationControl.SHOW_LOG);
        	if(showLog!=null)
        	{
        		showLog=showLog.toLowerCase();
        		if(showLog.equals("true"))
        		{
        			Tools.setShowLog(true);//开启打印LOG
        		}
        		else
        		{
        			Tools.setShowLog(false);//关闭打印LOG
        		}
        	}
            SilentInstallationControl mSAControl = SilentInstallationControl.getInstence();
            if(context!=null)
            {
            	mSAControl.setContext(context.getApplicationContext());
            }
            mSAControl.systemInit();
            if(action.equals(SilentInstallationControl.AppManagerParameter.INSTALL_APP))//安装应用
            {
                String appPath = intent.getStringExtra(SilentInstallationControl.AppManagerParameter.APP_PATH);
                boolean root = false;
                String type = intent.getStringExtra(SilentInstallationControl.AppManagerParameter.TYPE);
                if(type!=null&&type.toLowerCase().equals(SilentInstallationControl.AppManagerParameter.ROOT))
                {
                	root = true;
                }
                //root = true;
                boolean delete=intent.getBooleanExtra(SilentInstallationControl.AppManagerParameter.DELETE,false);
                int location = intent.getIntExtra(SilentInstallationControl.AppManagerParameter.INSTALL_LOCATION,0);
                //mSAControl.installApp(appPath,root,delete,location);
                new InstallAppHelper(context).installApp(context, appPath, delete, location);
            }
            else if(action.equals(SilentInstallationControl.AppManagerParameter.CANCEL_INSTALL_APP))//取消安装应用
            {
                boolean isCancelAll = intent.getBooleanExtra(SilentInstallationControl.AppManagerParameter.CANCEL_ALL, false);
                if(isCancelAll)
                {
                	mSAControl.removeAllInstallApp();//移除所有应用安装
                }
                else
                {
                	String cancelUniqueKey = intent.getStringExtra(SilentInstallationControl.AppManagerParameter.CANCEL_UNIQUEKEY);
                	if(cancelUniqueKey!=null&&!cancelUniqueKey.trim().equals(""))
                	{
                		mSAControl.removeInstallApp(cancelUniqueKey);//移除指定路径的应用安装
                	}
                	else
                	{
                		ArrayList<String> cancleList = intent.getStringArrayListExtra(SilentInstallationControl.AppManagerParameter.CANCEL_LIST);
                		if(cancleList!=null&&cancleList.size()>0)//移除应用安装列表
                		{
                			for(int i=cancleList.size()-1;i>=0;i--)
                			{
                				cancelUniqueKey = cancleList.get(i);
                				if(cancelUniqueKey!=null&&!cancelUniqueKey.trim().equals(""))
                            	{
                            		mSAControl.removeInstallApp(cancelUniqueKey);
                            	}
                			}
                		}
                	}
                }
            }
            else if(action.equals(SilentInstallationControl.AppManagerParameter.UNINSTALL_APP))//卸载应用
            {
                String appPackageName = intent.getStringExtra(SilentInstallationControl.AppManagerParameter.APP_PACKAGE_NAME);
                boolean root = false;
                String type = intent.getStringExtra(SilentInstallationControl.AppManagerParameter.TYPE);
                if(type!=null&&type.toLowerCase().equals(SilentInstallationControl.AppManagerParameter.ROOT))
                {
                	root = true;
                }
                //root = true;
                mSAControl.unInstallApp(appPackageName,root);
            }
            else if(action.equals(SilentInstallationControl.AppManagerParameter.CANCEL_UNINSTALL_APP))//取消卸载应用
            {
            	boolean isCancelAll = intent.getBooleanExtra(SilentInstallationControl.AppManagerParameter.CANCEL_ALL, false);
                if(isCancelAll)
                {
                	mSAControl.removeAllUninstallApp();//移除所有应用卸载
                }
                else
                {
                	String cancelUniqueKey = intent.getStringExtra(SilentInstallationControl.AppManagerParameter.CANCEL_UNIQUEKEY);
                	if(cancelUniqueKey!=null&&!cancelUniqueKey.trim().equals(""))
                	{
                		mSAControl.removeUninstallApp(cancelUniqueKey);//移除指定包名的应用卸载
                	}
                	else
                	{
                		ArrayList<String> cancleList = intent.getStringArrayListExtra(SilentInstallationControl.AppManagerParameter.CANCEL_LIST);
                		if(cancleList!=null&&cancleList.size()>0)//移除应用卸载列表
                		{
                			for(int i=cancleList.size()-1;i>=0;i--)
                			{
                				cancelUniqueKey = cancleList.get(i);
                				if(cancelUniqueKey!=null&&!cancelUniqueKey.trim().equals(""))
                            	{
                            		mSAControl.removeUninstallApp(cancelUniqueKey);
                            	}
                			}
                		}
                	}
                }
            }
            else if(action.equals(SilentInstallationControl.RunCommandParameter.RUN_COMMAND))//执行命令行
            {
                String id = intent.getStringExtra(SilentInstallationControl.RunCommandParameter.ID);
                ArrayList<String> commandList = intent.getStringArrayListExtra(SilentInstallationControl.RunCommandParameter.COMMAND_LIST);
                showLog("onStart", "commandList = "+Tools.showArrayList(commandList));
                boolean root = false;
                String type = intent.getStringExtra(SilentInstallationControl.AppManagerParameter.TYPE);
                if(type!=null&&type.toLowerCase().equals(SilentInstallationControl.AppManagerParameter.ROOT))
                {
                	root = true;
                }
                //root = true;
                mSAControl.runCommand(id,commandList,root);
            }
            else
            {
            }
        }
    }
}
